import { getProjectMemberSpaceAddList } from '../../../../api/index'

Component({
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    projectId: {
      type: String,
      value: ''
    },
    userId: {
      type: String,
      value: ''
    }
  },
  data: {
    list: []
  },
  methods: {
    beforeOpen () {
      this.getList()
    },
    select ({ currentTarget: { dataset: { item, index } } }) {
      let key = `list[${index}].checked`
      this.setData({
        [key]: !item.checked
      })
    },
    confirm () {
      let selections = this.data.list.filter(item => item.checked)
      if (selections.length) {
        this.triggerEvent('confirm', selections)
      } else {
        wx.showToast({ title: '请先选择要邀请的成员！', icon: 'none' })
      }
    },
    cancel () {
      this.triggerEvent('close')
    },
    async getList () {
      try {
        const { ResultSet } = await getProjectMemberSpaceAddList({ projectId: this.data.projectId, memberUserId: this.data.userId })
        ResultSet.userData.data.forEach(item => {
          item.checked = false
        })
        this.setData({ list: ResultSet.userData.data })
      } catch (err) {
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    }
  },
})